<template>
  <div style="height: 100%">
    <el-table v-loading="loading" :data="dataSource" border highlight-current-row @row-click="handleRowClick" :height="autoHeight">
      <el-table-column v-if="!select"  type="expand" label="资源" align="center">
        <template slot-scope="scope">
          <store-resource-table :config="scope.row" style="padding: 2px 8px" />
        </template>
      </el-table-column>
      <el-table-column prop="callCmd" label="存储指令" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="storeName" label="名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="storeType" label="存储类型" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-select v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" style="width: 100%" clearable @change="filterChange(scope.column.property)">
            <el-option v-for="option in storeTypes" :key="option.value" :value="option.value" :label="option.label" />
          </el-select>
        </template>
        <template slot-scope="scope">
          <span>{{ storeTypes.find(item => item.value === scope.row.storeType).label }}</span>
        </template>
      </el-table-column>
      <template v-if="!select">
        <el-table-column prop="accessHost" label="访问地址" align="center">
          <template slot="header" slot-scope="scope">
            <div>{{ scope.column.label }}</div>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="accessKey" label="访问用户" align="center">
          <template slot="header" slot-scope="scope">
            <div>{{ scope.column.label }}</div>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="accessBucket" label="访问入口" align="center">
          <template slot="header" slot-scope="scope">
            <div>{{ scope.column.label }}</div>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="storeRoot" label="根路径" align="center">
          <template slot="header" slot-scope="scope">
            <div>{{ scope.column.label }}</div>
            <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
          </template>
        </el-table-column>
      </template>
      <el-table-column prop="storeDef" label="是否默认" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.storeDef" type="primary">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100px">
        <template slot="header" slot-scope="scope" >
          <el-button v-if="!select" type="primary" @click="visible = true" icon="el-icon-circle-plus-outline">新增</el-button>
          <span v-else>操作</span>
        </template>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button type="success" icon="el-icon-edit" circle @click="() => { entityInfo=$Util.copy(scope.row);visible=true}" title="编辑"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>

    <el-dialog :visible.sync="visible" title="存储配置">
      <el-form label-position="right" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="entityInfo.storeName" placeholder="配置名称"/>
        </el-form-item>
        <el-form-item label="存储指令" required>
          <el-input v-model="entityInfo.callCmd" placeholder="存储指令"/>
        </el-form-item>
        <el-form-item label="存储类型" required>
          <el-select v-model="entityInfo.storeType" style="width: 100%" clearable>
            <el-option v-for="option in storeTypes" :key="option.value" :value="option.value" :label="option.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="根路径">
          <el-input v-model="entityInfo.storeRoot" placeholder="存储根路径"/>
        </el-form-item>
        <el-form-item label="访问地址">
          <el-input v-model="entityInfo.accessHost" placeholder="访问地址"/>
        </el-form-item>
        <el-form-item label="访问入口">
          <el-input v-model="entityInfo.accessBucket" placeholder="访问入口"/>
        </el-form-item>
        <el-form-item label="访问用户">
          <el-input v-model="entityInfo.accessKey" placeholder="访问用户"/>
        </el-form-item>
        <el-form-item label="访问令牌">
          <el-input v-model="entityInfo.accessSecret" placeholder="访问令牌"/>
        </el-form-item>
        <el-form-item v-if="entityInfo.storeType === 'FTP'" label="FTP模式">
          <el-select v-model="entityInfo.ftpMode" style="width: 100%" clearable>
            <el-option value="passive" label="被动" />
            <el-option value="active" label="主动" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否默认">
          <el-switch v-model="entityInfo.storeDef"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.storeId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";
import StoreResourceTable from "@/views/system/store/StoreResourceTable";

export default {
  name: "StoreTable",
  components: {StoreResourceTable},
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      storeTypes: [
        { value: 'LOCAL', label: '本地磁盘' }, { value: 'FTP', label: 'FTP' },
        { value: 'ALI_OSS', label: '阿里云OSS' }, { value: 'MINI', label: 'MINI IO' }
      ]
    }
  },
  computed:{
    autoHeight() {
      return this.select ? `300px` : `calc(100vh - 135px)`
    }
  },
  created() {
    this.queryUrl = '/storeConfig/queryPage'
    this.saveUrl = '/storeConfig/save'
    this.removeUrl = '/storeConfig/delete/'
    this.rowKey = 'storeId'
    this.loadSource()
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0 }
    }
  }
}
</script>

<style scoped>

</style>
